//
//  RBQiniuLoadTool.m
//  RMMaterialRed
//
//  Created by XBWH on 2024/3/9.
//

#import "RBQiniuLoadTool.h"
#import "QiniuSDK.h"
#import <AssetsLibrary/AssetsLibrary.h>

@implementation RBQiniuLoadTool
///单张图片上传
+(void)rb_uploadImageToQNFilePath:(UIImage *)filePat andAceicheToken:(NSString *)token andBuletName:(NSString *)BuletName success:(QNSuccessSignBlock)success failure:(QNFailureBlock)failure{
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    NSData *data;
    if (UIImagePNGRepresentation(filePat) == nil){
        data = UIImageJPEGRepresentation(filePat, 1);
    } else {
        data = UIImagePNGRepresentation(filePat);
    }
    [upManager putData:data key:[NSString stringWithFormat:@"%@%@",[self rb_getCurrentTime],BuletName] token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        if(info.ok){
            if (success) {
                success(key);
            }
            
        }
        else{
        }
    } option:nil];
}
///上传多张图片到七牛云
+(void)rb_uploadImageToQNFilePath:(NSArray *)photos andBuletName:(NSString *)BuletName andTokenStr:(NSString *)token success:(QNSuccessBlock)success failure:(QNFailureBlock)failure{
    NSMutableArray *imageAry =[NSMutableArray new];
    NSMutableArray *imageAdd = [NSMutableArray new];
    imageAry = photos.mutableCopy;
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    QNUploadOption *uploadOption = [[QNUploadOption alloc] initWithMime:nil
                                                        progressHandler:^(NSString *key, float percent) {
//        NSLog(@"上传进度 %.2f", percent);
    }
                                                                 params:nil
                                                               checkCrc:NO
                                                     cancellationSignal:nil];
    [imageAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSData *data;
        if (UIImagePNGRepresentation(obj) == nil){
            data = UIImageJPEGRepresentation(obj, 1);
        } else {
            data = UIImagePNGRepresentation(obj);
        }
        [upManager putData:data key:[NSString stringWithFormat:@"%ld%@%@",idx,[self rb_getCurrentTime],BuletName] token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
            if (info.isOK) {
                [imageAdd addObject:[NSString stringWithFormat:@"%@",resp[@"key"]]];
            }
            if (imageAdd.count == imageAry.count) {
                if (success) {
                    success(imageAdd);
                }
            }
        } option:uploadOption];
    }];
}
///上传视频
+(void)rb_uploadVideoToQNFilePath:(NSURL *)VideoString andAceicheToken:(NSString *)token andBuletName:(NSString *)BuletName success:(QNSuccessSignBlock)success failure:(QNFailureBlock)failure{
    MJWeakSelf
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    NSData *data = [NSData dataWithContentsOfURL:VideoString];
    [MBProgressHUD rb_showMessage:@"加载中..." ToView:RBWindow];
    [upManager putData:data key:[NSString stringWithFormat:@"%@%@%@",[self rb_generateTradeNO],[self rb_getCurrentTime],BuletName] token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        [MBProgressHUD rb_hideHUDForView:RBWindow];
        if(info.ok)
        {
            if (success) {
                success(key);
            }
        }
    } option:nil];
}
///上传音频
+(void)rb_uploadAmrToQNFilePath:(NSData *)AmrData andAceicheToken:(NSString *)token andBuletName:(NSString *)BuletName success:(QNSuccessSignBlock)success failure:(QNFailureBlock)failure{
    QNUploadManager *upManager = [[QNUploadManager alloc] init];
    [MBProgressHUD rb_showMessage:@"加载中..." ToView:RBWindow];
    [upManager putData:AmrData key:[NSString stringWithFormat:@"%@%@ios%@",[self rb_generateTradeNO],[self rb_getCurrentTime],BuletName] token:token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
        [MBProgressHUD rb_hideHUDForView:RBWindow];
        if(info.ok)
        {
            if (success) {
                success(key);
            }
        }
    } option:nil];
}
//获取当前时间
+(NSString *)rb_getCurrentTime {
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyMMddHHmmss"];
    NSString*dateTime = [formatter stringFromDate:[NSDate  date]];
    return dateTime;
}
+(NSString *)rb_generateTradeNO {
    static int kNumber = 8;
    NSString *sourceStr = @"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSMutableString *resultStr = [[NSMutableString alloc] init];
    srand((unsigned)time(0));
    for (int i = 0; i < kNumber; i++) {
        unsigned index = rand() % [sourceStr length];
        NSString *oneStr = [sourceStr substringWithRange:NSMakeRange(index, 1)];
        [resultStr appendString:oneStr];
    }
    return resultStr;
}
@end
